from backend.app.app.db.session_public import session_pub
import backend.app.app.models.db_owi_public_models as  md_pub

a ={
      "DvpUnit_OilStim_PlanProj_Name": "方案1",
      "DvpUnit_Name": "单元1",
      "Year": "2019",
      "StimType_Name": "类别2",
      "Stim_WellNumber_Valid": 1,
      "Stim_WellValidRatio": 1,
      "Stim_ValidPeriod_Average": 1,
      "Stim_IncreOil_Average": 0.3
    }


#   修改数据
def modify_data(data:dict):
    try:
        session_pub.query(md_pub.TbYearDvpunitOilstimPlanprojForecast) \
            .filter(md_pub.TbYearDvpunitOilstimPlanprojForecast.DvpUnit_OilStim_PlanProj_Name == data["DvpUnit_OilStim_PlanProj_Name"],
                    md_pub.TbYearDvpunitOilstimPlanprojForecast.DvpUnit_Name == data["DvpUnit_Name"],
                    md_pub.TbYearDvpunitOilstimPlanprojForecast.Year == data["Year"],
                    md_pub.TbYearDvpunitOilstimPlanprojForecast.StimType_Name == data["StimType_Name"]).update({
            md_pub.TbYearDvpunitOilstimPlanprojForecast.Year : data["Year"],
            md_pub.TbYearDvpunitOilstimPlanprojForecast.Stim_WellNumber_Valid: data["Stim_WellNumber_Valid"],
            md_pub.TbYearDvpunitOilstimPlanprojForecast.Stim_WellValidRatio: data["Stim_WellValidRatio"],
            md_pub.TbYearDvpunitOilstimPlanprojForecast.Stim_ValidPeriod_Average: data["Stim_ValidPeriod_Average"],
            md_pub.TbYearDvpunitOilstimPlanprojForecast.Stim_IncreOil_Average: data["Stim_IncreOil_Average"]
        })
        session_pub.commit()
        print('更新数据成功')
        return True
    except:
        print('更新数据失败')
        return False

#   删除数据
def delete_data(data:dict):
    try:
        session_pub.query(md_pub.TbYearDvpunitOilstimPlanprojForecast) \
            .filter(md_pub.TbYearDvpunitOilstimPlanprojForecast.DvpUnit_OilStim_PlanProj_Name == data["DvpUnit_Name"],
                    md_pub.TbYearDvpunitOilstimPlanprojForecast.DvpUnit_Name == data["DvpUnit_OilStim_PlanProj_Name"],
                    md_pub.TbYearDvpunitOilstimPlanprojForecast.Year == data["DvpUnit_OilStim_PlanProj_Name"],
                    md_pub.TbYearDvpunitOilstimPlanprojForecast.StimType_Name == data["DvpUnit_OilStim_PlanProj_Name"]).delete()
        session_pub.commit()
        print('删除数据成功')
        return True
    except:
        print('删除数据失败')
        return False

#   新增数据
def add_data(data:dict):
    try:
        session_pub.add(md_pub.TbYearDvpunitOilstimPlanprojForecast(DvpUnit_OilStim_PlanProj_Name=data["DvpUnit_OilStim_PlanProj_Name"],
                                                                   DvpUnit_Name=data["DvpUnit_Name"],
                                                                   Year=data["Year"],
                                                                   StimType_Name=data["StimType_Name"],
                                                                   Stim_WellNumber_Valid=data["Stim_WellNumber_Valid"],
                                                                   Stim_WellValidRatio=data["Stim_WellValidRatio"],
                                                                   Stim_ValidPeriod_Average=data["Stim_ValidPeriod_Average"],
                                                                   Stim_IncreOil_Average=data["Stim_IncreOil_Average"]))
        session_pub.commit()
        print('储存成功')
        return True
    except:
        print('储存失败')
        session_pub.rollback()
        return False

#   通过条件查询结果
def get_lib(name,type,start,end):
    try:
        result = session_pub.query(md_pub.TbYearDvpunitOilstimPlanprojForecast.DvpUnit_OilStim_PlanProj_Name,
                                   md_pub.TbYearDvpunitOilstimPlanprojForecast.DvpUnit_Name,
                                   md_pub.TbYearDvpunitOilstimPlanprojForecast.Year,
                                   md_pub.TbYearDvpunitOilstimPlanprojForecast.StimType_Name,
                                   md_pub.TbYearDvpunitOilstimPlanprojForecast.Stim_WellNumber_Valid,
                                   md_pub.TbYearDvpunitOilstimPlanprojForecast.Stim_WellValidRatio,
                                   md_pub.TbYearDvpunitOilstimPlanprojForecast.Stim_ValidPeriod_Average,
                                   md_pub.TbYearDvpunitOilstimPlanprojForecast.Stim_IncreOil_Average
                                   ).filter(md_pub.TbYearDvpunitOilstimPlanprojForecast.DvpUnit_Name == name,
                                            md_pub.TbYearDvpunitOilstimPlanprojForecast.StimType_Name == type,
                                            md_pub.TbYearDvpunitOilstimPlanprojForecast.Year >= start,
                                            md_pub.TbYearDvpunitOilstimPlanprojForecast.Year <= end).\
        order_by(md_pub.TbYearDvpunitOilstimPlanprojForecast.Year).all()
        return result
    except:
        print('查找失败')
        session_pub.rollback()
        return False